#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

import sys
import argparse
sys.path.append('/usr/lib/selfnetd')
from sn_param import SnParam
from sn_subcmd import SnSubCmdMain


def parseArgs():
    argParser = argparse.ArgumentParser()
    subParsers = argParser.add_subparsers()

    apGenCaCert = subParsers.add_parser("generate-ca-cert")
    apGenCaCert.set_defaults(subcmd="gen_ca_cert")

    apGenMyCert = subParsers.add_parser("generate-my-cert")
    apGenMyCert.set_defaults(subcmd="gen_my_cert")

    apGenCert = subParsers.add_parser("generate-cert")
    apGenCert.set_defaults(subcmd="gen_cert")
    apGenCert.add_argument("--hostname", required=True)
    apGenCert.add_argument("--outdir")

    apListPeer = subParsers.add_parser("list-peers")
    apListPeer.set_defaults(subcmd="list_peers")

    apOpPowerOn = subParsers.add_parser("poweron")
    apOpPowerOn.set_defaults(subcmd="poweron")
    apOpPowerOn.add_argument("peerName")

    apOpPowerOff = subParsers.add_parser("poweroff")
    apOpPowerOff.set_defaults(subcmd="poweroff")
    apOpPowerOff.add_argument("peerName")

    apOpReboot = subParsers.add_parser("reboot")
    apOpReboot.set_defaults(subcmd="reboot")
    apOpReboot.add_argument("peerName")

    apOpSuspend = subParsers.add_parser("suspend")
    apOpSuspend.set_defaults(subcmd="suspend")
    apOpSuspend.add_argument("peerName")

    apOpHibernate = subParsers.add_parser("hibernate")
    apOpHibernate.set_defaults(subcmd="hibernate")
    apOpHibernate.add_argument("peerName")

    apOpHybridSleep = subParsers.add_parser("hybrid-sleep")
    apOpHybridSleep.set_defaults(subcmd="hybrid-sleep")
    apOpHybridSleep.add_argument("peerName")

    apListModule = subParsers.add_parser("list-modules")
    apListModule.set_defaults(subcmd="list_modules")

    return argParser.parse_args()

################################################################################

param = SnParam()
parseResult = parseArgs()

# some assistant sub command
if parseResult.subcmd == "gen_ca_cert":
    SnSubCmdMain(param).generateCaCert()
elif parseResult.subcmd == "gen_my_cert":
    SnSubCmdMain(param).generateMyCert()
elif parseResult.subcmd == "gen_cert":
    SnSubCmdMain(param).generateCert(parseResult.hostname, parseResult.outdir)
elif parseResult.subcmd == "list_peers":
    SnSubCmdMain(param).listPeers()
elif parseResult.subcmd in ["poweron", "poweroff", "reboot", "suspend", "hibernate", "hybrid-sleep"]:
    SnSubCmdMain(param).peerPowerOperation(parseResult.peerName, parseResult.subcmd)
elif parseResult.subcmd == "list_modules":
    SnSubCmdMain(param).listModules()
else:
    assert False

sys.exit(0)
